<template>
    <!-- <h2>登录</h2> -->
    <div class="LoginBg">
      <el-form :model="form" :rules="rules" ref="loginForm" class="login" label-width="60px">
          <h2>用户登录</h2>
          <br>
          <el-form-item label="账号:" prop="username">
              <el-input ref="username" v-model="form.username"  prefix-icon="el-icon-user" placeholder="请输入用户名">
              </el-input>
          </el-form-item>
          <el-form-item label="密码:"  prop="password">
              <el-input ref="password" type="password" v-model="form.password" prefix-icon="el-icon-lock"  show-password placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item class="login-btns">
                  <el-button class="login-btns-1" type="primary" @click="submit">登录</el-button>
                  <el-button type="danger" @click="$router.push('/register')">注册</el-button>
          </el-form-item>
      </el-form> 
    </div>
</template>
<script>
export default {
  data () {
    return {
      form: {},
      rules: {
        username: { required: true, message: '请输入账号' },
        password: { required: true, message: '请输入密码' }
      }
    }
  },
  methods: {
    submit () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$http.post('admin/login', { ...this.form }).then(res => {
            if (res.err > 0) {
              this.$message.error('用户或者密码错误')
              this.$refs.password.clear()
              this.$refs.password.focus()
            } else {
              this.$notify({ title: '恭喜！登录成功', type: 'success' })
              localStorage.setItem('Oppo-Token', res.token)
              this.$router.push('/app')
            }
          })
        }
      })
    }
  },
  mounted () {
    this.$refs.username.focus()
    //敲回车登录
    window.onkeydown = (event) => {
      if(event.keyCode === 13) this.submit()
    }
  }
}
</script>

<style>
.login-btns{
    margin: center;
    margin-top: 20px;
    /* background-color: red; */
}
</style>

<style lang="less" scopend>
.LoginBg{
  position: static;
  .login{
    position: relative;
    width: 320px;
    left: 80%;
    top: 50%;
    transform: translate(-50%,-50%);
    box-shadow: 0 0 16px rgba(0,0,0,.08);
    background: white;
    border-radius: 8px;
    padding: 20px 30px;
    h2{
        text-align: center;
        font-size: 24px;
        margin-bottom: 20px;
        color: black;
    }
}
}

</style>
